﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:马星俊
// ===============================================================================

using System;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmDeptEdit : BaseForm
    {
        public FrmDeptEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        private async void FrmDeptEdit_Load(object sender, EventArgs e)
        {
            await layoutControl.BindingModelAsync();
        }

        private async void BtnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        private void LayoutPanel_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () =>
            {
                if (!WinFormHelper.IsRootValue(e.Args.ParentId))
                {
                    var list = await SysDeptService.Instance.SelectListAsync(new SysDeptFilter());
                    deptTreeListLookUpEdit1.Properties.DataSource = list;
                    deptTreeListLookUpEdit1.Enabled = !WinFormHelper.IsRootValue(e.Args.ParentId);//如果上级是根节点,则不允许修改
                }

                bindingSource.DataSource = await SysDeptService.Instance.GetOrCreateAsync(e.Args);
                WinFormHelper.SetTreeRootValue(deptTreeListLookUpEdit1, e.ParentId);
            };
        }

        private void LayoutPanel_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = async () => await SysDeptService.Instance.SaveAsync(e.Args.Created, (SysDept)e.Model);
        }

        private async void LayoutPanel_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysDeptService.Instance.NewAsync(layoutControl.Args.ParentId);
            await layoutControl.BindingModelAsync();
        }
    }
}